package com.sankuai.inf.leaf.server.service;



import com.sankuai.inf.leaf.segment.model.User;
import com.sankuai.inf.leaf.server.CommonException;
import com.sankuai.inf.leaf.server.util.MD5;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


/**
 * Created by huangds on 2017/10/28.
 */
@Service
public class LoginService {

    public final static String ADMIN_ROLE = "admin";
    @Autowired
    private UserService userService;

    public static void main(String[] args) {
        System.out.printf( MD5.authHash("admin", "13poc14KZb"));
    }

  public boolean login(User user) throws CommonException {
          User userByUserName = userService.getUserByUserName(user.getUsername());
          if (ObjectUtils.isEmpty(userByUserName)){
              throw new CommonException(100502,"没有此用户");
          }
          //加密密码
          String hash = MD5.authHash(user.getPassword(), userByUserName.getSalt());
          if (!StringUtils.equals(userByUserName.getPassword(),hash)){
              throw new CommonException(100503,"密码错误");
          }
          return true;
      }
}
